﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    internal class PreviewRenderUtility
    {
        public Camera m_Camera;
        public float m_CameraFieldOfView;
        public Light[] m_Light;
        internal RenderTexture m_RenderTexture;
        internal RenderTexture m_RenderTextureGammaCorrect;
        private SavedRenderTargetState m_SavedState;
        private Rect m_TargetRect;

        public PreviewRenderUtility() : this(false)
        {
        }

        public PreviewRenderUtility(bool renderFullScene)
        {
            this.m_CameraFieldOfView = 15f;
            this.m_Light = new Light[2];
            System.Type[] components = new System.Type[] { typeof(Camera) };
            GameObject obj2 = EditorUtility.CreateGameObjectWithHideFlags("PreRenderCamera", HideFlags.HideAndDontSave, components);
            this.m_Camera = obj2.camera;
            this.m_Camera.fieldOfView = this.m_CameraFieldOfView;
            this.m_Camera.enabled = false;
            this.m_Camera.clearFlags = CameraClearFlags.Depth;
            this.m_Camera.farClipPlane = 10f;
            this.m_Camera.nearClipPlane = 2f;
            this.m_Camera.backgroundColor = new Color(0.1921569f, 0.1921569f, 0.1921569f, 1f);
            this.m_Camera.renderingPath = RenderingPath.Forward;
            if (!renderFullScene)
            {
                Handles.SetCameraOnlyDrawMesh(this.m_Camera);
            }
            for (int i = 0; i < 2; i++)
            {
                System.Type[] typeArray2 = new System.Type[] { typeof(Light) };
                GameObject obj3 = EditorUtility.CreateGameObjectWithHideFlags("PreRenderLight", HideFlags.HideAndDontSave, typeArray2);
                this.m_Light[i] = obj3.light;
                this.m_Light[i].type = LightType.Directional;
                this.m_Light[i].intensity = 0.5f;
                this.m_Light[i].enabled = false;
            }
            this.m_Light[0].color = SceneView.kSceneViewFrontLight;
            this.m_Light[1].transform.rotation = Quaternion.Euler(340f, 218f, 177f);
            this.m_Light[1].color = (Color) (new Color(0.4f, 0.4f, 0.45f, 0f) * 0.7f);
        }

        public void BeginPreview(Rect r, GUIStyle previewBackground)
        {
            this.InitPreview(r);
            if ((previewBackground != null) && (previewBackground != GUIStyle.none))
            {
                Graphics.DrawTexture(previewBackground.overflow.Add(new Rect(0f, 0f, (float) this.m_RenderTexture.width, (float) this.m_RenderTexture.height)), previewBackground.normal.background, new Rect(0f, 0f, 1f, 1f), previewBackground.border.left, previewBackground.border.right, previewBackground.border.top, previewBackground.border.bottom, new Color(0.5f, 0.5f, 0.5f, 1f), null);
            }
        }

        public void BeginStaticPreview(Rect r)
        {
            this.InitPreview(r);
            Color color = new Color(0.3215686f, 0.3215686f, 0.3215686f, 1f);
            Texture2D texture = new Texture2D(1, 1);
            texture.SetPixel(0, 0, color);
            texture.Apply();
            Graphics.DrawTexture(new Rect(0f, 0f, (float) this.m_RenderTexture.width, (float) this.m_RenderTexture.height), texture);
            UnityEngine.Object.DestroyImmediate(texture);
        }

        public void Cleanup()
        {
            if (this.m_Camera != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_Camera.gameObject, true);
            }
            if (this.m_RenderTexture != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_RenderTexture);
                this.m_RenderTexture = null;
            }
            if (this.m_RenderTextureGammaCorrect != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_RenderTextureGammaCorrect);
                this.m_RenderTextureGammaCorrect = null;
            }
            foreach (Light light in this.m_Light)
            {
                if (light != null)
                {
                    UnityEngine.Object.DestroyImmediate(light.gameObject, true);
                }
            }
        }

        public void DrawMesh(Mesh mesh, Matrix4x4 matrix, Material mat, int subMeshIndex)
        {
            Graphics.DrawMesh(mesh, matrix, mat, 1, this.m_Camera, subMeshIndex);
        }

        public void DrawMesh(Mesh mesh, Vector3 pos, Quaternion rot, Material mat, int subMeshIndex)
        {
            Graphics.DrawMesh(mesh, pos, rot, mat, 1, this.m_Camera, subMeshIndex);
        }

        public Texture EndPreview()
        {
            this.GammaBlit();
            this.m_SavedState.Restore();
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            return ((QualitySettings.activeColorSpace != ColorSpace.Linear) ? this.m_RenderTexture : this.m_RenderTextureGammaCorrect);
        }

        public Texture2D EndStaticPreview()
        {
            this.GammaBlit();
            RenderTexture source = (QualitySettings.activeColorSpace != ColorSpace.Linear) ? this.m_RenderTexture : this.m_RenderTextureGammaCorrect;
            RenderTexture dest = RenderTexture.GetTemporary((int) this.m_TargetRect.width, (int) this.m_TargetRect.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
            Graphics.Blit(source, dest);
            RenderTexture.active = dest;
            Texture2D textured = new Texture2D((int) this.m_TargetRect.width, (int) this.m_TargetRect.height, TextureFormat.RGB24, false, true);
            textured.ReadPixels(new Rect(0f, 0f, this.m_TargetRect.width, this.m_TargetRect.height), 0, 0);
            textured.Apply();
            RenderTexture.ReleaseTemporary(dest);
            this.m_SavedState.Restore();
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            return textured;
        }

        private void GammaBlit()
        {
            if (QualitySettings.activeColorSpace == ColorSpace.Linear)
            {
                Material gammaCorrectMaterialNoClip = EditorGUI.gammaCorrectMaterialNoClip;
                if (Unsupported.IsDeveloperBuild())
                {
                    gammaCorrectMaterialNoClip = new Material(gammaCorrectMaterialNoClip);
                }
                Graphics.Blit(this.m_RenderTexture, this.m_RenderTextureGammaCorrect, gammaCorrectMaterialNoClip);
                if ((gammaCorrectMaterialNoClip != null) && Unsupported.IsDeveloperBuild())
                {
                    UnityEngine.Object.DestroyImmediate(gammaCorrectMaterialNoClip);
                }
            }
        }

        private void InitPreview(Rect r)
        {
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            this.m_TargetRect = r;
            int width = (int) r.width;
            int height = (int) r.height;
            if ((((this.m_RenderTexture == null) || (this.m_RenderTexture.width != width)) || ((this.m_RenderTexture.height != height) || (this.m_RenderTextureGammaCorrect == null))) || ((this.m_RenderTextureGammaCorrect.width != width) || (this.m_RenderTextureGammaCorrect.height != height)))
            {
                if (this.m_RenderTexture != null)
                {
                    UnityEngine.Object.DestroyImmediate(this.m_RenderTexture);
                    this.m_RenderTexture = null;
                }
                if (this.m_RenderTextureGammaCorrect != null)
                {
                    UnityEngine.Object.DestroyImmediate(this.m_RenderTextureGammaCorrect);
                    this.m_RenderTextureGammaCorrect = null;
                }
                float a = Mathf.Max(Mathf.Min(width * 2, 0x400), width) / width;
                float b = Mathf.Max(Mathf.Min(height * 2, 0x400), height) / height;
                float num5 = Mathf.Min(a, b);
                bool flag = QualitySettings.activeColorSpace == ColorSpace.Linear;
                this.m_RenderTexture = new RenderTexture((int) (width * num5), (int) (height * num5), 0x10, RenderTextureFormat.ARGB32, !flag ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB);
                this.m_RenderTexture.hideFlags = HideFlags.HideAndDontSave;
                this.m_RenderTextureGammaCorrect = new RenderTexture((int) (width * num5), (int) (height * num5), 0x10, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                this.m_RenderTextureGammaCorrect.hideFlags = HideFlags.HideAndDontSave;
                this.m_Camera.targetTexture = this.m_RenderTexture;
            }
            float num6 = (this.m_RenderTexture.width > 0) ? Mathf.Max((float) 1f, (float) (((float) this.m_RenderTexture.height) / ((float) this.m_RenderTexture.width))) : 1f;
            this.m_Camera.fieldOfView = (Mathf.Atan(num6 * Mathf.Tan((this.m_CameraFieldOfView * 0.5f) * 0.01745329f)) * 57.29578f) * 2f;
            this.m_SavedState = new SavedRenderTargetState();
            EditorGUIUtility.SetRenderTextureNoViewport(this.m_RenderTexture);
            GL.LoadOrtho();
            GL.LoadPixelMatrix(0f, (float) this.m_RenderTexture.width, (float) this.m_RenderTexture.height, 0f);
            ShaderUtil.rawViewportRect = new Rect(0f, 0f, (float) this.m_RenderTexture.width, (float) this.m_RenderTexture.height);
            ShaderUtil.rawScissorRect = new Rect(0f, 0f, (float) this.m_RenderTexture.width, (float) this.m_RenderTexture.height);
            GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
        }
    }
}

